/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.amu.wmi.kino.visualjavafx.javafxcodegenerators.main;

import pl.edu.amu.wmi.kino.visualjavafx.javafxcodegenerators.api.*;
import DummyJavaFXCode.DummyConstantCodeGenerator;
import java.io.IOException;
import org.openide.util.Lookup;
import pl.edu.amu.wmi.kino.visualjavafx.model.objects.Application;

/**
 *
 * @author Admin
 */

//TODO : Grzegorz : methods and fields of this class should not be static, thread-safety issue

public final class MainGenerator {

    private static boolean isGenerating=false;
    private static Application currentModel=null;

    private static StringBuilder sb = new StringBuilder();
    private MainGenerator(){};
    
    private static Lookup.Result<Generator> generators = Lookup.getDefault().lookupResult(Generator.class);

    public static boolean isGeneratingNow(){
        return isGenerating;
    }
    
    public static void generateNext(Object o,int recursionLevel,Object original){
        for(Generator g: generators.allInstances()){
            if(g.canGenerate(o)){
                g.generate(sb, o,recursionLevel,original);
                return;
            }
        }
        //TODO : Grzegorz : uncomment when all generators will be implemented
        //throw new UnsupportedOperationException("There is no generator for this object"+o.class.getName());
    }
    public static String generateAll(Application app) throws IOException{
        //dummy code for testing
        isGenerating=true;
        currentModel=app;
        DummyConstantCodeGenerator dummy= new DummyConstantCodeGenerator();
        sb.append(dummy.generateBegin());
        MainGenerator.generateNext(app.getFrames().get(0),0,app.getFrames());
        sb.append(dummy.generateEnd());
        //end of dummy code for testing
        String retVal = sb.toString();
        sb = new StringBuilder();
        isGenerating=false;
        currentModel=null;
        return retVal;
    }

}
